package com.homework.ch11;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;



import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;

class Person implements Serializable {
    private static final long serialVersionUID = 4258798319750620830L;
    private String name;
    private String gender;

    public Person() {}

    public Person(String name, String gender) {
        this.name = name;
        this.gender = gender;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getGender() {
        return gender;
    }

    public void setGender(String gender) {
        this.gender = gender;
    }

    @Override
    public String toString() {
        return "Person [name=" + name + ", gender=" + gender + "]";
    }
}

public class Test029_ObjectStream {
    private final static String FILE_PATH = "D:/test/obj_file";

    public static void save() {
        // 创建三个Person对象
        Person p1 = new Person("张三", "男");
        Person p2 = new Person("李四", "男");
        Person p3 = new Person("王五", "女");

        ObjectOutputStream oos = null;
        try {
            // 构建对象输出流，关联目标文件
            oos = new ObjectOutputStream(new FileOutputStream(FILE_PATH));
            // 序列化写入三个对象
            oos.writeObject(p1);
            oos.writeObject(p2);
            oos.writeObject(p3);
            System.out.println("对象保存成功！");
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            // 关闭流释放资源
            if (oos != null) {
                try {
                    oos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    public static void load() {
        ObjectInputStream ois = null;
        try {
            // 构建对象输入流，关联目标文件
            ois = new ObjectInputStream(new FileInputStream(FILE_PATH));
            // 反序列化读取三个对象（顺序与写入一致）
            Person p1 = (Person) ois.readObject();
            Person p2 = (Person) ois.readObject();
            Person p3 = (Person) ois.readObject();
            // 打印对象属性
            System.out.println("读取到的对象：");
            System.out.println(p1);
            System.out.println(p2);
            System.out.println(p3);
        } catch (IOException | ClassNotFoundException e) {
            e.printStackTrace();
        } finally {
            // 关闭流释放资源
            if (ois != null) {
                try {
                    ois.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    public static void main(String[] args) {
        save();
        load();
    }
}